package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.IShoppingCartService;
import com.itheima.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    IShoppingCartService shoppingCartService;

    //添加购物车
    @PostMapping("/add")
    private R<String> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);

        Long dishId = shoppingCart.getDishId();
        //1.判断是套餐还是菜品
        if (dishId != null){
            //说明点的是菜品
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //点的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询是否是第一次点该菜,如果是第一次点就添加,反而修改
        ShoppingCart isShoppingCart = shoppingCartService.getOne(wrapper);
        if (isShoppingCart == null){
            //说明第一次点餐(添加)
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
        }else {
            //修改
            isShoppingCart.setNumber(isShoppingCart.getNumber() +1 );
            shoppingCartService.updateById(isShoppingCart);
        }
        return R.success("添加成功");
    }

    //查询购物车
    @GetMapping("/list")
    private R<List<ShoppingCart>> selectAllCart()   {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        List<ShoppingCart> shoppingCarts = shoppingCartService.list(wrapper);

        return R.success(shoppingCarts);
    }

    //减少数量
    @PostMapping("/sub")
    @Transactional
    R<String> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        Long setMealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();

        if (dishId !=null){
            //查询该菜品

            wrapper.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart cart = shoppingCartService.getOne(wrapper);
            cart.setNumber(cart.getNumber() - 1);
            //更新
            shoppingCartService.updateById(cart);

            //再查一次
            ShoppingCart cart2 = shoppingCartService.getOne(wrapper);
            if (cart2.getNumber() <= 0){
                //说明不可以再减了,删除该菜品
                shoppingCartService.remove(wrapper);
            }
        }

        if (setMealId !=null){
            //查询该菜品
            wrapper.eq(ShoppingCart::getSetmealId,setMealId);
            ShoppingCart cart = shoppingCartService.getOne(wrapper);
            cart.setNumber(cart.getNumber() - 1);
            //更新
            shoppingCartService.updateById(cart);

            //再查一次
            ShoppingCart cart2 = shoppingCartService.getOne(wrapper);;
            if (cart2.getNumber() <= 0){
                //说明不可以再减了,删除该菜品
                shoppingCartService.remove(wrapper);
            }
        }
        return R.success("删除成功");
    }

    //清空购物车
    @DeleteMapping("/clean")
    private R<String> clean(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }
}
